﻿#region Licence
// Copyright (c) 2011 BAX Services Bartosz Adamczewski
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSpec.Shell.Display;
using Shell.Resources;
using CSpec.Testing;
using System.IO;

namespace CSpec.Shell.Execution.Commands
{
    /// <summary>
    /// Writes detailed information from the console to a file,
    /// provided by a given file path.
    /// </summary>
    public class FileCommand : ICommand
    {
        /// <summary>
        /// Initializes the default constructor for the Command.
        /// </summary>
        public FileCommand()
        {}

        /// <summary>
        /// Initializes the default constructor for the Command, that takes the priority.
        /// </summary>
        /// <param name="priority">prioririty is used to tell the Action that it need's to be processed first or last.</param>
        public FileCommand(int priority)
        {
            this.Priority = priority;
        }

        /// <summary>
        /// Gets the priority of the command.
        /// </summary>
        /// <remarks>
        /// Prioririty is used to tell the Action that it 
        /// need's to be processed first or last.
        /// </remarks>
        public int Priority
        {
            get;
            private set;
        }
       
        /// <summary>
        /// Gets the key for this command. 
        /// </summary>
        public string Key
        {
            get { return "-f"; }
        }

        /// <summary>
        /// Gets the description for this command.
        /// </summary>
        public string Description
        {
            get { return CommandMessages.FileAll; }
        }

        /// <summary>
        /// Executes the command.
        /// Hooks all runner events to the console to display all possible information
        /// from the runner.
        /// </summary>
        /// <param name="console"></param>
        /// <param name="actionParams"></param>
        public void Execute(CSpecConsole console, params object[] actionParams)
        {
            //actionParams[0] will be some kind of runner action.
            //since this command is generic it will not care what that is as long
            //as it's the first param.

            string consoleText = console.GetConsoleHistory();
            string path = string.Empty;

            if (actionParams.Length > 1)
            {
                path = String.IsNullOrEmpty(actionParams[1].ToString())
                    ? GetDefaultPath()
                    : actionParams[1].ToString();
            }
            else
                path = GetDefaultPath();

            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                using (TextWriter tw = new StreamWriter(fs))
                {
                    tw.Write(consoleText);
                }
            }

        }

        /// <summary>
        /// Gets the default path formatting when no file was specified.
        /// </summary>
        /// <returns></returns>
        private string GetDefaultPath()
        {
            DateTime dt = DateTime.Now;
            return string.Format("Test {0}_{1}_{2}_{3}_{4}_{5}.cpt", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
        }
    }
}
